﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BuiltInToURPTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 文件夹或文件路径(选择界面选中文件夹)
        /// </summary>
        private string[] shaderPath;
        private List<string> shaderFiles = new List<string>();

        /// <summary>
        /// 选择路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SelecPath_Click(object sender, EventArgs e)
        {
            this.GetSelectPath();
        }

        /// <summary>
        /// 开始转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Start_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.lab_path.Text))
            {
                MessageBox.Show("路径不符合规范，请重新输入", "提示~");
                return;
            }

            var list = FilesTool.GetFiles(this.shaderPath);
            BuildInToURPTool.ReplaceShaderByPath(list, () => 
            {
                MessageBox.Show("替换完成","提示~");
            });
        }

        /// <summary>
        /// 获取选择的路径
        /// </summary>
        private void GetSelectPath()
        {
            if (this.checkFile.Checked)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Multiselect = true;
                openFileDialog.Title = "请选择文件";
                openFileDialog.Filter = "所有文件(*shader*)|*.shader*";  //所有文件(*cginc*)|*.cginc
                openFileDialog.ShowDialog();
                if (openFileDialog.FileNames.Length > 0)
                {
                    this.shaderPath = new string[openFileDialog.FileNames.Length - 1];
                    this.shaderPath = openFileDialog.FileNames;
                    this.lab_path.Text = "";
                    foreach (var path in this.shaderPath)
                    {
                        this.lab_path.Text += path + " ";
                    }
                }
            }
            else
            {
                Ookii.Dialogs.VistaFolderBrowserDialog folderBrowser = new Ookii.Dialogs.VistaFolderBrowserDialog();
                folderBrowser.Description = "请选择路径";
                folderBrowser.ShowDialog();
                if (!string.IsNullOrEmpty(folderBrowser.SelectedPath))
                {
                    this.shaderPath = new string[1];
                    this.shaderPath[0] = folderBrowser.SelectedPath;
                    this.lab_path.Text = this.shaderPath[0];
                }
            }
        }
    }
}
